package com.app.twth.ui.cyclecount;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.b2cpick.submit.SKUBarcodes;
import com.app.twth.ui.cyclecount.location.CycleCountLocationQr;
import com.app.twth.ui.cyclecount.sku.CycleCountSkuLPN;
import com.app.twth.ui.cyclecount.sku.CycleCountSkuLPNQr;
import com.app.twth.utils.Utils;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class CycleCountViewModel extends BaseAndroidViewModel<CycleCountDataModel> {
    public MutableLiveData<CycleCountSku> cycleCountSkuLiveData = new MutableLiveData<>();
    public MutableLiveData<CycleCountSkuLPN> cycleCountSkuLpnLiveData = new MutableLiveData<>();
    public MutableLiveData<CycleCountLocationQr> cycleCountLocationQrLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> submitLiveData = new MutableLiveData<>();
    public MutableLiveData<SKUBarcodes> skuBarcodesLiveData = new MutableLiveData<>();

    public CycleCountViewModel(@NonNull Application application) {
        super(application);
    }

    public void cycleCountSku(String barcode) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.cycleCountSku(barcode, new JsonCallBack<BaseResponseEntity<CycleCountSku>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<CycleCountSku>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<CycleCountSku> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    CycleCountSku data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setSku(barcode);
                    cycleCountSkuLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void cycleCountSkuInfo(String ibLpn, String barcode) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.cycleCountSkuInfo(ibLpn, barcode, new JsonCallBack<BaseResponseEntity<CycleCountSkuLPN>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<CycleCountSkuLPN>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<CycleCountSkuLPN> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    CycleCountSkuLPN data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    if (data.getBarcodeList() == null) {
                        data.setBarcodeList(new ArrayList<>());
                    }
                    data.getBarcodeList().add(barcode);
                    if (!Utils.isEmpty(data.getSku())) {
                        data.getBarcodeList().add(data.getSku());
                    }
                    cycleCountSkuLpnLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void cycleCountSkuSubmit(CycleCountSkuLPNQr cycleCountSkuLPNQr) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.cycleCountSkuSubmit(cycleCountSkuLPNQr, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitLiveData.setValue(true);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void cycleCountLocationInventory(String locationCode, String ibLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.cycleCountLocationInventory(locationCode, ibLpn, new JsonCallBack<BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    CycleCountLocationQr cycleCountLocationQr = new CycleCountLocationQr();
                    List<CycleCountLocationQr.SkuListDTO> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    cycleCountLocationQr.setLocationCode(locationCode);
                    cycleCountLocationQr.setIbLpn(ibLpn);
                    cycleCountLocationQr.setSkuList(data);
                    cycleCountLocationQrLiveData.setValue(cycleCountLocationQr);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void cycleCountLocationLpnSubmit(CycleCountLocationQr cycleCountLocationQr) {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.cycleCountLocationLpnSubmit(cycleCountLocationQr, new JsonCallBack<BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<CycleCountLocationQr.SkuListDTO>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    cycleCountLocationQr.setSkuList(baseResponseEntity.data);
                    cycleCountLocationQrLiveData.setValue(cycleCountLocationQr);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void cycleCountLocationSubmit(CycleCountLocationQr cycleCountLocationQr) {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.cycleCountLocationSubmit(cycleCountLocationQr, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitLiveData.setValue(true);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void getInfoByBarcode(String sku) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.getInfoByBarcode(sku, new JsonCallBack<BaseResponseEntity<SKUBarcodes>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<SKUBarcodes>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<SKUBarcodes> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    skuBarcodesLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }


}
